require "Task"
require "TaskCategory/RandomTask"
require "WeightPool"

QNW_TS_AttentionToTheMap = QNW_TS_Task:derive("QNW_TS_AttentionToTheMap")

local this = QNW_TS_AttentionToTheMap
this.name = getText("IGUI_QNW_TaskSystem_Task_AttentionToTheMap_Name")
this.desc = getText("IGUI_QNW_TaskSystem_Task_AttentionToTheMap_Desc")
this.taskCategory = QNW_TS_RandomTask

function QNW_TS_AttentionToTheMap:isComplete()
    return (not SafeHouse.isSafeHouse(getPlayer():getCurrentSquare(), nil, true)) and self:isTime()
end

function QNW_TS_AttentionToTheMap:complete()
    if QNW_TS_TaskWindow.instance then
        QNW_TS_TaskWindow.instance:destroy()
    end
    QNW_TS_Task.complete(self)
    local player = getPlayer()
    for i = 1, 10 do
        QNW_TS_ClientCommand.addZombie(player:getX() + ZombRand(18, 24), player:getY(), 0, 1, 1)
    end
    for i = 1, 10 do
        QNW_TS_ClientCommand.addZombie(player:getX() - ZombRand(18, 24), player:getY(), 0, 1, 1)
    end
    for i = 1, 10 do
        QNW_TS_ClientCommand.addZombie(player:getX(), player:getY() + ZombRand(18, 24), 0, 1, 1)
    end
    for i = 1, 10 do
        QNW_TS_ClientCommand.addZombie(player:getX(), player:getY() - ZombRand(18, 24), 0, 1, 1)
    end
    SoundAlleart(player, player:getX(), player:getY(), player:getZ())
    local warn = getText("IGUI_AttentionToTheMap_Warn")
    processGeneralMessage(warn)
end

function QNW_TS_AttentionToTheMap:isAbandon()
    return self:isTime()
end

function QNW_TS_AttentionToTheMap:getProgress()
    return ""
end

function QNW_TS_AttentionToTheMap:registerClient(_player)
    _player:Say(getText("IGUI_QNW_TaskSystem_Get", self.name))
end

function QNW_TS_AttentionToTheMap:save()
    local save = {}
    save.Type = self.Type
    save.uuid = self.uuid
    save.time = self.time
    save.rewards = {}
    for k, v in pairs(self.rewards) do
        table.insert(save.rewards, v:save())
    end
    return save
end

function QNW_TS_AttentionToTheMap:load(_table)
    self.uuid = _table.uuid
    self.time = _table.time
    if _table.rewards then
        self:loadReward(_table.rewards)
    end
end

function QNW_TS_AttentionToTheMap:new()
    local o = QNW_TS_Task:new()
    setmetatable(o, self)
    self.__index = self

    o.uuid = getRandomUUID()
    o.time = getGameTime():getCalender():getTimeInMillis() + 60 * 60 * 2000

    return o
end

function QNW_TS_AttentionToTheMap:initReward()
    QNW_TS_ClientCommand.randomReward(self, "height", 2)
end

function SoundAlleart(_player, _x, _y, _z)
    local sbvoice = tostring("sbvoice")
    local PULSE_VOLUME = 500
    getWorldSoundManager():addSound(_player, _x, _y, _z, 500, PULSE_VOLUME);
    getWorldSoundManager():addSound(_player, _x, _y, _z, 500, PULSE_VOLUME);
    getWorldSoundManager():addSound(_player, _x, _y, _z, 500, PULSE_VOLUME);
    getWorldSoundManager():addSound(_player, _x, _y, _z, 500, PULSE_VOLUME);
    getSoundManager():PlayWorldSound(sbvoice, _player:getSquare(), 0, 0, 0, false);
end

